home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap04.txt < prev    next >
Text File  |  1992-01-18  |  22KB  |  472 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 4
  8.                                                         FUNCTIONS
  9.  
  10. This chapter discusses enhancements in the capabilities of
  11. functions that have been made to C++.  These changes make
  12. programming more convenient and permit the compiler to do further
  13. checking for errors.  A fair amount of time is also spent in this
  14. chapter teaching the modern form of function definition and
  15. prototyping.
  16.  
  17. Prototyping allows the compiler to do additional type checking for
  18. your function calls which can detect some programming errors.  The
  19. first two example programs in this chapter are designed to teach
  20. prototyping and what it will do for you.  Prototyping is a
  21. relatively new addition to C, so even some experienced C
  22. programmers are not familiar with it.  If you have experience with
  23. prototyping you can skip directly to the section named PASS BY
  24. REFERENCE on page 4-4 of this chapter.
  25.  
  26.  
  27. PROTOTYPES
  28. _________________________________________________________________
  29.  
  30. Examine the file named PROTYPE1.CPP for our      ================
  31. first look at a prototype and an illustration of   PROTYPE1.CPP
  32. how it is used.  The prototyping used in C++ is  ================
  33. no different than that used in ANSI-C.
  34. Actually, many C programmers take a rather dim
  35. view of prototyping and seem reluctant to use it, but with C++ it
  36. is considerably more important and is in much heavier use.  In
  37. fact, prototyping is required to be used in some situations in C++.
  38.  
  39. A prototype is a limited model of a more complete entity to come
  40. later.  In this case, the full function is the complete entity to
  41. come later and the prototype is illustrated in line 4.  The
  42. prototype gives a model of the interface to the function that can
  43. be used to check the calls to the function for the proper number
  44. of parameters and the correct types of parameters.  Each call to
  45. the function named do_stuff() must have exactly three parameters
  46. or the compiler will give an error message.  In addition to the
  47. correct number of parameters, the types must be compatible or the
  48. compiler will issue an error message.  Notice that when the
  49. compiler is working on lines 12 and 13, the type checking can be
  50. done based on the prototype in line 4 even though the function
  51. itself is not yet defined.  If the prototype is not given, the
  52. number of parameters will not be checked, nor will the types of the
  53. parameters be checked.  Even if you have the wrong number of
  54. parameters, you will get an apparently good compile and link, but
  55. the program may do some very strange things when it is executed.
  56.  
  57.  
  58.                                                          Page 4-1
  59.  
  60.                                             Chapter 4 - Functions
  61.  
  62.  
  63. To write the prototype, simply copy the header from the function
  64. to the beginning of the program and append a semicolon to the end
  65. as a signal to the compiler that this is not a function but a
  66. prototype.  The variable names given in the prototype are optional
  67. and act merely as comments to the program reader since they are
  68. completely ignored by the compiler.  You could replace the variable
  69. name wings in line 4 with your first name and there would be no
  70. difference in compilation.  Of course, the next person that had to
  71. read your program would be somewhat baffled with your choice of
  72. variable names.
  73.  
  74. In this case, the two function calls to this function, given in
  75. lines 12 and 13, are correct so no error will be listed during
  76. compilation.
  77.  
  78. Even though we wish to use the char type for eyes in the function,
  79. we wish to use it as a number rather than as a character.  The cast
  80. to int in line 20 is required to force the printout of the
  81. numerical value rather than an ASCII character.  The next example
  82. program is similar but without the cast to int.
  83.  
  84.  
  85.  
  86. COMPATIBLE TYPES
  87. _________________________________________________________________
  88.  
  89. We mentioned compatible types earlier so we should review them just
  90. a bit in order to make our discussion of prototyping complete.
  91. Compatible types are any simple types that can be converted from
  92. one to another in a meaningful way.  For example, if you used an
  93. integer as the actual parameter and the function was expecting a
  94. float type as the formal parameter, the system would do the
  95. conversion automatically, without mentioning it to you.  This is
  96. also true of a float changing to a char, or a char changing to an
  97. int.  There are definite conversion rules which would be followed.
  98. These rules are given in great detail in section 3.2 of the draft
  99. of the ANSI-C standard and are also given on page 198 of the second
  100. edition of the K&R reference.
  101.  
  102. If we supplied a pointer to an integer as the actual parameter and
  103. expected an integer as the formal parameter in the function, the
  104. conversion would not be made because they are two entirely
  105. different kinds of values.  Likewise, a structure would not be
  106. converted automatically to a long float, an array, or even to a
  107. different kind of structure, they are all incompatible and cannot
  108. be converted in any meaningful manner.  The entire issue of type
  109. compatibility as discussed in chapter 2 of this tutorial applies
  110. equally well to the compatibility of types when calling a function.
  111. Likewise, the type specified as the return type, in this case void,
  112. must be compatible with the expected return type in the calling
  113. statement, or the compiler will issue a warning.
  114.  
  115.  
  116.  
  117.                                                          Page 4-2
  118.  
  119.                                             Chapter 2 - Functions
  120.  
  121.  
  122. HOW DOES PROTOTYPING WORK?
  123. _________________________________________________________________
  124.  
  125. This is your chance to try prototyping for yourself and see how
  126. well it works and what kinds of error messages you get when you do
  127. certain wrong things.  Change the actual parameters in line 12 to
  128. read (12.2, 13, 12345) and see what the compiler says about that
  129. change.  It will probably say nothing because they are all type
  130. compatible.  If you change it to read (12.0, 13), it will issue a
  131. warning or error because there are not enough arguments given.
  132. Likewise you should receive an error message if you change one of
  133. the parameters in line 13 to an address by putting an ampersand in
  134. front of one of the variable names.  Finally, change the first word
  135. in line 4 from void to int and see what kind of error message is
  136. given.  You will first be required to make the function header in
  137. line 16 agree with the prototype, then you will find that there is
  138. not a variable returned from the function.  You should have a good
  139. feeling that prototyping is doing something good for you after
  140. making these changes.
  141.  
  142. Be sure to compile and execute this program then make the changes
  143. recommended above, attempting to compile it after each change.
  144.  
  145.  
  146.  
  147. A LITTLE MORE PROTOTYPING
  148. _________________________________________________________________
  149.  
  150. Examine the next example program named           ================
  151. PROTYPE2.CPP for a little more information on      PROTYPE2.CPP
  152. prototyping.  This program is identical to the   ================
  153. last one except for a few small changes.  The
  154. variable names have been omitted from the
  155. prototype in line 4 merely as an illustration that they are
  156. interpreted as comments by the C++ compiler.  The function header
  157. is formatted differently to allow for a comment alongside each of
  158. the actual parameters.  This should make the function header a
  159. little more self explanatory.  However, you should remember that
  160. comments should not be used to replace careful selection of
  161. variable names.  In this particular case, the comments add
  162. essentially nothing to the clarity of the program.
  163.  
  164.  
  165.  
  166. WHAT DOES PROTOTYPING COST?
  167. _________________________________________________________________
  168.  
  169. Prototyping is essentially free because it costs absolutely nothing
  170. concerning the run time size or speed of execution.  Prototyping
  171. is a compile time check and slows down the compile time a
  172. negligible amount because of the extra checking that the compiler
  173. must do.  If prototyping finds one er